package com.aike.service;

import com.aike.dao.PlaceMapper;
import com.aike.pojo.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PlaceServiceImpl implements PlaceService{

    private PlaceMapper placeMapper;

    public void setPlaceMapper(PlaceMapper placeMapper) {
        this.placeMapper = placeMapper;
    }

    public String addHouse(String houseName, Integer type, Integer floorNum) {
        int v = placeMapper.addHouse(houseName, type, floorNum);
        JSONObject jsonObject = new JSONObject();
        if(v > 0)
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(200, "增加成功"));
        }
        else
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(400, "增加失败"));
        }

        return jsonObject.toJSONString();
    }

    public String addRoom(Integer houseId, String roomName, Integer floorPos, Integer colNum, Integer rowNum) {
        JSONObject jsonObject = new JSONObject();
        int seatNum = colNum * rowNum;
        int v = placeMapper.addRoom(houseId, roomName, floorPos, colNum, rowNum, seatNum);
        if(v > 0)
        {
            List<Seat> list = new ArrayList<Seat>();
            int roomId = placeMapper.selectRoomIdByHouseIdAndRoomName(houseId, roomName);
            for(int i = 1; i <= seatNum; i++)
            {
                list.add(new Seat(0, roomId, i, 0, "", 0));
            }
            placeMapper.addSeat(list);

            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(200, "增加成功"));

        }
        else
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(400, "增加失败"));
        }

        return jsonObject.toJSONString();
    }

    //    查找位置
    public String selectSearchSeat(Integer type) {
        List<SearchHouse> listHouse = placeMapper.selectSearchHouse(type);
        List<SearchFloor> listFloor = placeMapper.selectSearchFloor();
        List<SearchRoom> listRoom = placeMapper.selectSearchRoom();
        //循环添加到所有内容和位置中
        //房子添加楼层，楼层添加房间
        for(SearchHouse h1 : listHouse)
        {
            List<SearchFloor> list1 = new ArrayList<SearchFloor>();
            for(SearchFloor f1 : listFloor)
            {
                if(f1.getHouseId() == h1.getHouseId())
                {
                    List<SearchRoom> list2 = new ArrayList<SearchRoom>();
                    for(SearchRoom r1 : listRoom)
                    {
                        if(r1.getFloorPos() == f1.getFloorPos())
                        {
                            list2.add(r1);
                        }
                    }
                    f1.setChildren(list2);

                    list1.add(f1);
                }

            }
            h1.setChildren(list1);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", listHouse);
        jsonObject.put("meta", new Meta(200, "获取成功"));
        return jsonObject.toJSONString();
    }

}
